<template>
  <a-modal
    :visible="visible"
    title="内容发布"
    width="70%"
    @cancel="close"
    :footer="null">
    <a-row type="flex" :gutter="16">
      <a-col flex="2">
        <a-tabs @change="tabChange" :active-key="queryParam.contentType">
          <a-tab-pane tab="文章" key="1"></a-tab-pane>
          <a-tab-pane tab="视频" key="2"></a-tab-pane>
          <a-tab-pane tab="音频" key="3"></a-tab-pane>
          <!-- <a-tab-pane tab="h5" key="4"></a-tab-pane> -->
        </a-tabs>
        <a-form-model layout="inline" class="j-inline-form">
          <a-form-model-item>
            <a-input-search v-model="queryParam.contentTitle" placeholder="请输入内容标题"></a-input-search>
          </a-form-model-item>
          <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
          <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
        </a-form-model>
        <a-table
          size="middle"
          rowKey="contentNo"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys, onChange: onSelectChange, type:'checkbox'}"
          class="j-table-force-nowrap"
          @change="handleTableChange">
        </a-table>
      </a-col>
      <a-col flex="1">
        <publish-platform ref="platform" :content-type="queryParam.contentType"></publish-platform>
        <a-button :loading="publishing" type="danger" @click="publish"
                  style="display:block;margin:20px auto 0;width: 50%;">发布
        </a-button>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
  import {JeecgListMixin} from "@/mixins/JeecgListMixin";
  import PublishPlatform from "@/components/xinke/PublishPlatform";
  import {postAction} from "@/api/manage";

  export default {
    name: "ContentPublishModel",
    components: {PublishPlatform},
    props: ['visible'],
    mixins: [JeecgListMixin],
    data() {
      return {
        queryParam: {
          contentType: '1'
        },
        url: {
          list: '/content/contentInfo/list'
        },
        columns: [
          {
            title: '标题',
            align: 'center',
            dataIndex: 'contentTitle'
          },
          {
            title: '类型',
            align: 'center',
            dataIndex: 'contentType'
          },
          {
            title: '发布者',
            align: 'center',
            dataIndex: 'authorName'
          },
          {
            title: '发布时间',
            align: 'center',
            dataIndex: 'releaseTime'
          },
        ],
        selectedRowKeys: [],
        selectedRows: [],
        publishing: false
      }
    },
    methods: {
      tabChange(key) {
        this.queryParam.contentType = key;
        this.loadData(1);
      },
      close() {
        this.$emit('update:visible', false);
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectedRows = selectedRows;
      },
      publish() {
        const platforms = this.$refs.platform.getSelectedPlatform();
        if (!platforms || platforms.length === 0) {
          this.$message.warning('请先勾选要发布平台');
          return;
        }
        if (!this.selectedRows || this.selectedRows.length === 0) {
          this.$message.warning('请先勾选要发布的内容');
          return;
        }
        this.publishing = true;
        platforms.forEach(item => {
          let promiseList = [];
          const platformInfo = item.selectedOption;
          if (item.name == '微信') {
            promiseList.push(this.publishWeiXin(platformInfo, this.selectedRows))
          } else if (item.name == '微博') {
            promiseList.push(this.publishWeiBo(platformInfo, this.selectedRows))
          }
          Promise.all(promiseList).then(resList => {
            console.log(resList);
          }).finally(() => {
            this.publishing = false;
          })
        });
      },
      //微信发布
      publishWeiXin(thirdInfo, contentList) {
        return postAction('publish/third/weixinShare', {thirdInfo, contentList});
      },
      //微博发布
      publishWeiBo(thirdInfo, contentList) {
        let list = [];
        contentList.forEach(item=>{
          list.push({
            title:item.contentTitle,
            content:item.content,
            cover:item.mbLogoUrl,
            summary:item.contentIntroduce,
            text:item.contentTitle,
          })
        });
        return postAction('publish/third/weiBoShare', {thirdInfo, contentList:list});
      },
      //抖音发布
      publishDouYin(thirdInfo, contentList){
        let list = [];
        contentList.forEach(item=>{
          list.push({
            filePath:item.contentTitle,
            fileName:item.content
          })
        });
        return postAction('publish/third/douYinShare',{thirdInfo,contentList:list})
      }
    }
  }
</script>

<style scoped>

</style>